<template>
    <div class="cqg_wrap">
        <div class="page-content">
            <div class="top-panel">
                <div class="panel-item">
                    <div class="item-stat">
                        <div>
                            <span class="item-stat-num">
                                <span v-for="item in bottlePressure[0]">{{ item }}</span>
                            </span>
                            <span class="item-stat-unit">MPA</span>
                            <div class="item-stat-label">1号瓶组</div>
                        </div>
                    </div>
                    <div class="item-label">20MPa储氢瓶组压力</div>
                </div>

                <div class="panel-item">
                    <div class="item-stat">
                        <div>
                            <span class="item-stat-num">
                                <span v-for="item in bottlePressure[1]">{{ item }}</span>
                            </span>
                            <span class="item-stat-unit">MPA</span>
                            <div class="item-stat-label">2号瓶组</div>
                        </div>
                        <div>
                            <span class="item-stat-num">
                                <span v-for="item in bottlePressure[2]">{{ item }}</span>
                            </span>
                            <span class="item-stat-unit">MPA</span>
                            <div class="item-stat-label">3号瓶组</div>
                        </div>
                    </div>
                    <div class="item-label">45MPa储氢瓶组压力</div>
                </div>

                <div class="panel-item">
                    <div class="item-stat">
                        <div>
                            <span class="item-stat-num">
                                <span v-for="item in bottlePressure[3]">{{ item }}</span>
                            </span>
                            <span class="item-stat-unit">MPA</span>
                            <div class="item-stat-label">4号瓶组</div>
                        </div>
                        <div>
                            <span class="item-stat-num">
                                <span v-for="item in bottlePressure[4]">{{ item }}</span>
                            </span>
                            <span class="item-stat-unit">MPA</span>
                            <div class="item-stat-label">5号瓶组</div>
                        </div>
                        <div>
                            <span class="item-stat-num">
                                <span v-for="item in bottlePressure[5]">{{ item }}</span>
                            </span>
                            <span class="item-stat-unit">MPA</span>
                            <div class="item-stat-label">6号瓶组</div>
                        </div>
                    </div>
                    <div class="item-label">20MPa储氧瓶组压力</div>
                </div>
            </div>


            <div class="middle-panel">
                <div class="panel-item">
                    <div class="panel-title">20MPa储氢瓶组压力</div>
                    <div class="panel-content">
                        <div class="flow-box">
                            <!--7-->
                            <span :class="`flow-valve ${ valveState[2] ? 'is_close' : '' }`"></span>
                            <span class="flow-text">1号瓶组</span>
                            <!--8-->
                            <span class="flow-value">{{ valveState[16445] | formatFlowValue }} MPa</span>
                        </div>

                        <div class="chart-box" ref="lineChart1"></div>
                    </div>
                </div>

                <div class="panel-item">
                    <div class="panel-title">45MPa储氢瓶组压力</div>
                    <div class="panel-content">
                        <div class="flow-box">
                            <!--9-->
                            <span :class="`flow-valve ${ valveState[4] ? 'is_close' : '' }`"></span>
                            <!--11-->
                            <span :class="`flow-valve ${ valveState[6] ? 'is_close' : '' }`"></span>
                            <span class="flow-text">2号瓶组</span>
                            <span class="flow-text">3号瓶组</span>
                            <!--10-->
                            <span class="flow-value">{{ valveState[16445] | formatFlowValue }} MPa</span>
                            <!--12-->
                            <span class="flow-value">{{ valveState[16445] | formatFlowValue }} MPa</span>
                        </div>

                        <div class="chart-box" ref="lineChart2"></div>
                    </div>
                </div>
            </div>


            <div class="bottom-panel">
                <div class="panel-title">20MPa储氢瓶组压力</div>
                <div class="panel-content">
                    <div class="flow-box">
                        <!--16-->
                        <span :class="`flow-valve ${ valveState[2] ? 'is_close' : '' }`"></span>
                        <!--20-->
                        <span :class="`flow-valve ${ valveState[6] ? 'is_close' : '' }`"></span>
                        <!--18-->
                        <span :class="`flow-valve ${ valveState[4] ? 'is_close' : '' }`"></span>
                        <span class="flow-text">4号瓶组</span>
                        <span class="flow-text">5号瓶组</span>
                        <span class="flow-text">6号瓶组</span>
                        <!--17-->
                        <span class="flow-value">{{ valveState[16445] | formatFlowValue }} MPa</span>
                        <!--21-->
                        <span class="flow-value">{{ valveState[16445] | formatFlowValue }} MPa</span>
                        <!--19-->
                        <span class="flow-value">{{ valveState[16445] | formatFlowValue }} MPa</span>
                    </div>

                    <div class="chart-box" ref="lineChart3"></div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                valveState: {},
                bottlePressure: [],
            }
        },
        methods: {
            getData() {
                this.$api.queryTempquery(
                    [
                        {
                            target: 'h2-01',
                            address: [
                                16442,
                                16443,
                                16444,
                                16445,
                                16442,
                                16445,
                                2,
                                16445,
                                4,
                                16445,
                                6,
                                16445,
                                2,
                                16445,
                                4,
                                16445,
                                6,
                                16445,
                                ],
                        }
                    ]
                ).then(( res )=> {
                    var obj = {};

                    for( var j=0; j<res.length; j++ ) {
                        var data = res[j].result || [];

                        for( var i=0; i<data.length; i++ ) {
                            obj[ data[i].address ] = data[i].value;
                        }
                    }

                    var bottlePressure = ['','','','','','',];

                    /*1*/
                    bottlePressure[0] = obj[16442] ? (obj[16442].toFixed( 3 ) + '').slice(0,4) : '----';

                    /*2*/
                    bottlePressure[1] = obj[16443] ? (obj[16443].toFixed( 3 ) + '').slice(0,4) : '----';

                    /*3*/
                    bottlePressure[2] = obj[16444] ? (obj[16444].toFixed( 3 ) + '').slice(0,4) : '----';

                    /*4*/
                    bottlePressure[3] = obj[16445] ? (obj[16445].toFixed( 3 ) + '').slice(0,4) : '----';

                    /*5*/
                    bottlePressure[4] = obj[16442] ? (obj[16442].toFixed( 3 ) + '').slice(0,4) : '----';

                    /*6*/
                    bottlePressure[5] = obj[16445] ? (obj[16445].toFixed( 3 ) + '').slice(0,4) : '----';


                    this.valveState = obj;
                    this.bottlePressure = bottlePressure;
                    console.log("res==>",obj)
                })
            },

            /**
             * 获取折线图数据
             */
            getLineChartData() {
                var startDate = this.formatDate( 'YYYY-mm-dd', new Date() ) + ' 00:00:00';
                var endDate = this.formatDate( 'YYYY-mm-dd HH:MM:SS', new Date() );

                /*13*/
                var api1 = this.$api.queryCharts(
                    {
                        Name: 'h2-01_pv_power',
                        selector: 'MEAN',
                        begin: startDate,
                        end: endDate,
                        groupBlock: '15m',
                    }
                );

                /*14*/
                var api2 = this.$api.queryCharts(
                    {
                        Name: 'h2-01_pv_power',
                        selector: 'MEAN',
                        begin: startDate,
                        end: endDate,
                        groupBlock: '15m',
                    }
                );

                /*15*/
                var api3 = this.$api.queryCharts(
                    {
                        Name: 'h2-01_utility_power',
                        selector: 'MEAN',
                        begin: startDate,
                        end: endDate,
                        groupBlock: '15m',
                    }
                );

                /*22*/
                var api4 = this.$api.queryCharts(
                    {
                        Name: 'h2-01_pv_power',
                        selector: 'MEAN',
                        begin: startDate,
                        end: endDate,
                        groupBlock: '15m',
                    }
                );

                /*23*/
                var api5 = this.$api.queryCharts(
                    {
                        Name: 'h2-01_utility_power',
                        selector: 'MEAN',
                        begin: startDate,
                        end: endDate,
                        groupBlock: '15m',
                    }
                );

                /*24*/
                var api6 = this.$api.queryCharts(
                    {
                        Name: 'h2-01_pv_power',
                        selector: 'MEAN',
                        begin: startDate,
                        end: endDate,
                        groupBlock: '15m',
                    }
                );

                Promise.all( [api1,api2,api3,api4,api5,api6] ).then(( res )=> {
                    console.log('res==>',res);
                    var names = [];
                    var value = [[],[],[],[],[],[],];

                    for( var i=0; i<res.length; i++ ) {

                        for( var j=0; j<res[i].data.length; j++ ) {
                            if( i===0 ) {
                                names.push( this.formatDate( 'HH:MM', new Date( res[i].data[j][0] ) ) );
                            }

                            value[i].push( parseInt( res[i].data[j][1] ) )
                        }
                    }

                    this.chartLoad(
                        {
                            names: names,
                            value: [
                                value[0],
                            ],
                            id: 'lineChart1',
                            legend: ['1号瓶组', ],
                            title: '压力（MPa）',
                        }
                    );


                    this.chartLoad(
                        {
                            names: names,
                            value: [
                                value[1],
                                value[2],
                            ],
                            id: 'lineChart2',
                            legend: ['2号瓶组', '3号瓶组', ],
                            title: '排油压力（MPa）',
                        }
                    );


                    this.lineChartLoad(
                        {
                            names: names,
                            value: [
                                value[3],
                                value[4],
                                value[5],
                            ],
                            id: 'lineChart3',
                            legend: ['4号瓶组', '5号瓶组', '6号瓶组', ],
                            title: '排油压力（MPa）',
                        }
                    );

                })
            },

            chartLoad( obj ) {
                var _this = this;
                var chart = this.$echarts.init( this.$refs[ obj.id ] );

                var option = {
                    title: {
                        text: obj.title,
                        left: '2%',
                        top: '13%',
                        textStyle: {
                            color: '#333333',
                            fontSize: 16,
                        }
                    },
                    grid: {
                        top: '30%',
                        right: '3%',
                        bottom: '3%',
                        left: '4%',
                        containLabel: true,
                    },
                    tooltip: {
                        trigger: 'axis',
                        // formatter: '{b}:{c}',
                    },
                    legend: {
                        show: 'true',
                        data: obj.legend,
                        right: '3%',
                        top: '13%',
                        icon: 'circle',
                        itemGap: 25,
                        textStyle: {
                            fontSize: 14,
                            color: '#9A9FAF',
                        },
                    },
                    color: ['#35C759','#6EF690'],
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            axisLabel: {
                                formatter: '{value}',
                                fontSize: 14,
                                margin: 13,
                                textStyle: {
                                    color: '#9A9FAF',
                                },
                            },
                            axisLine: {
                                show: false,
                            },
                            splitLine: {
                                show: false,
                                lineStyle: {
                                    color: '#243753',
                                },
                            },
                            axisTick: {
                                show: false,
                            },
                            data: obj.names,
                        },
                    ],
                    yAxis: [
                        {
                            boundaryGap: false,
                            type: 'value',
                            axisLabel: {
                                fontSize: 14,
                                margin: 13,
                                textStyle: {
                                    color: '#9A9FAF',
                                },
                            },
                            splitLine: {
                                lineStyle: {
                                    color: '#e3e3e3',
                                    type: 'dotted',
                                },
                            },
                            axisLine: {
                                show: false,
                                lineStyle: {
                                    color: '#283352',
                                },
                            },
                            axisTick: {
                                show: false,
                            },
                        },
                    ],
                    series: [
                        {
                            name: obj.legend[0],
                            type: 'line',
                            // smooth: true,
                            // showSymbol: false,
                            lineStyle: {
                                normal: {
                                    width: 1.5,
                                    color: '#35C759',
                                },
                            },
                            areaStyle: {
                                normal: {
                                    color: new _this.$echarts.graphic.LinearGradient( 0, 0, 0, 1,
                                        [
                                            {
                                                offset: 0,
                                                color: 'rgba(53,199,89,0.11)',
                                            },
                                            {
                                                offset: 0.8,
                                                color: 'rgba(53,199,89,0)',
                                            },
                                        ],
                                        false
                                    ),
                                },
                            },
                            data: obj.value[0],
                        },
                        {
                            show: obj.legend[1] ? true : false,
                            name: obj.legend[1],
                            type: 'line',
                            // smooth: true,
                            // showSymbol: false,
                            lineStyle: {
                                normal: {
                                    width: 1.5,
                                    color: '#6EF690',
                                },
                            },
                            areaStyle: {
                                normal: {
                                    color: new _this.$echarts.graphic.LinearGradient( 0, 0, 0, 1,
                                        [
                                            {
                                                offset: 0,
                                                color: 'rgba(110,246,144,0.11)',
                                            },
                                            {
                                                offset: 0.8,
                                                color: 'rgba(110,246,144,0)',
                                            },
                                        ],
                                        false
                                    ),
                                },
                            },
                            data: obj.value[1],
                        },
                    ],
                };

                chart.setOption( option );
            },

            lineChartLoad( obj ) {
                var _this = this;
                var chart = this.$echarts.init( this.$refs[ obj.id ] );

                var option = {
                    title: {
                        text: obj.title,
                        left: '2%',
                        top: '2%',
                        textStyle: {
                            color: '#333333',
                            fontSize: 16,
                        }
                    },
                    grid: {
                        top: '18%',
                        right: '3%',
                        bottom: '3%',
                        left: '4%',
                        containLabel: true,
                    },
                    tooltip: {
                        trigger: 'axis',
                        // formatter: '{b}:{c}',
                    },
                    legend: {
                        show: 'true',
                        data: obj.legend,
                        right: '3%',
                        top: '2%',
                        icon: 'circle',
                        itemGap: 25,
                        textStyle: {
                            fontSize: 14,
                            color: '#9A9FAF',
                        },
                    },
                    color: ['#AF52DE','#CF97EB','#E7CBF5'],
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            axisLabel: {
                                formatter: '{value}',
                                fontSize: 14,
                                margin: 13,
                                textStyle: {
                                    color: '#9A9FAF',
                                },
                            },
                            axisLine: {
                                show: false,
                            },
                            splitLine: {
                                show: false,
                                lineStyle: {
                                    color: '#243753',
                                },
                            },
                            axisTick: {
                                show: false,
                            },
                            data: obj.names,
                        },
                    ],
                    yAxis: [
                        {
                            boundaryGap: false,
                            type: 'value',
                            axisLabel: {
                                fontSize: 14,
                                margin: 13,
                                textStyle: {
                                    color: '#9A9FAF',
                                },
                            },
                            splitLine: {
                                lineStyle: {
                                    color: '#e3e3e3',
                                    type: 'dotted',
                                },
                            },
                            axisLine: {
                                show: false,
                                lineStyle: {
                                    color: '#283352',
                                },
                            },
                            axisTick: {
                                show: false,
                            },
                        },
                    ],
                    series: [
                        {
                            name: obj.legend[0],
                            type: 'line',
                            // smooth: true,
                            // showSymbol: false,
                            lineStyle: {
                                normal: {
                                    width: 1.5,
                                    color: '#AF52DE',
                                },
                            },
                            areaStyle: {
                                normal: {
                                    color: new _this.$echarts.graphic.LinearGradient( 0, 0, 0, 1,
                                        [
                                            {
                                                offset: 0,
                                                color: 'rgba(175,82,222,0.11)',
                                            },
                                            {
                                                offset: 0.8,
                                                color: 'rgba(175,82,222,0)',
                                            },
                                        ],
                                        false
                                    ),
                                },
                            },
                            data: obj.value[0],
                        },
                        {
                            name: obj.legend[1],
                            type: 'line',
                            // smooth: true,
                            // showSymbol: false,
                            lineStyle: {
                                normal: {
                                    width: 1.5,
                                    color: '#CF97EB',
                                },
                            },
                            areaStyle: {
                                normal: {
                                    color: new _this.$echarts.graphic.LinearGradient( 0, 0, 0, 1,
                                        [
                                            {
                                                offset: 0,
                                                color: 'rgba(207,151,235,0.11)',
                                            },
                                            {
                                                offset: 0.8,
                                                color: 'rgba(207,151,235,0)',
                                            },
                                        ],
                                        false
                                    ),
                                },
                            },
                            data: obj.value[1],
                        },
                        {
                            name: obj.legend[2],
                            type: 'line',
                            // smooth: true,
                            // showSymbol: false,
                            lineStyle: {
                                normal: {
                                    width: 1.5,
                                    color: '#E7CBF5',
                                },
                            },
                            areaStyle: {
                                normal: {
                                    color: new _this.$echarts.graphic.LinearGradient( 0, 0, 0, 1,
                                        [
                                            {
                                                offset: 0,
                                                color: 'rgba(231,203,245,0.11)',
                                            },
                                            {
                                                offset: 0.8,
                                                color: 'rgba(231,203,245,0)',
                                            },
                                        ],
                                        false
                                    ),
                                },
                            },
                            data: obj.value[2],
                        },
                    ],
                };

                chart.setOption( option );
            },
        },
        mounted() {
            this.getData();
            this.getLineChartData();
        }
    }
</script>

<style lang="less">
    .cqg_wrap {
        width: 100%;
        height: 100%;
        position: relative;

        >.page-content {
            width: 100%;
            height: 1306px;
            padding-bottom: 28px;

            .flow-valve {
                position: absolute;
                width: 24px;
                height: 22px;
                background-image: url("../../assets/images/cqg/icon_valve_open.png");
                background-size: 100% 100%;

                &.is_close {
                    background-image: url("../../assets/images/cqg/icon_valve_close.png");
                }
            }

            .flow-text {
                position: absolute;
                font-size: 16px;
                color: #535763;
            }

            .flow-value {
                color: #333333;
                font-size: 14px;
                position: absolute;
                padding: 0px 10px;
                box-sizing: border-box;
                min-width: 80px;
                line-height: 28px;
                background-color: rgba(249, 250, 254, 0.8);
                border-radius: 4px;
                border: 1px solid #dddddd;
                text-align: center;

                &:after {
                    content: ' ';
                    position: absolute;
                    bottom: -8px;
                    left: 0;
                    right: 0;
                    margin: auto;
                    width: 0;
                    height: 0;
                    border-left: 5px solid transparent;
                    border-right: 5px solid transparent;
                    border-top: 8px solid #F9FAFE;
                }

                &:before {
                    content: ' ';
                    position: absolute;
                    bottom: -10px;
                    left: 0;
                    right: 0;
                    margin: auto;
                    width: 0;
                    height: 0;
                    border-left: 6px solid transparent;
                    border-right: 6px solid transparent;
                    border-top: 10px solid #dddddd;
                }
            }

            >.top-panel {
                height: 220px;
                width: 100%;
                margin-bottom: 28px;

                >.panel-item {
                    display: inline-block;
                    vertical-align: top;
                    height: 100%;
                    margin-right: 28px;
                    background: #FFFFFF;
                    border-radius: 8px;
                    padding: 40px;
                    box-sizing: border-box;
                    text-align: center;

                    .item-stat {
                        width: 100%;
                        display: flex;
                        justify-content: space-between;

                        >div {
                            display: inline-block;
                            vertical-align: top;

                            .item-stat-num {
                                display: inline-block;
                                vertical-align: bottom;
                                height: 70px;
                                width: 140px;
                                font-size: 36px;
                                font-weight: 600;
                                color: #FFFFFF;
                                line-height: 60px;
                                background-image: url("../../assets/images/cqg/bg_num.png");
                                background-repeat: no-repeat;
                                background-position: center center;
                                box-sizing: border-box;

                                >span {
                                    display: inline-block;
                                    vertical-align: top;
                                    width: 32px;
                                    height: 60px;
                                    margin-right: 4px;

                                    &:last-child {
                                        margin-right: 0;
                                    }
                                }
                            }

                            .item-stat-unit {
                                display: inline-block;
                                vertical-align: bottom;
                                margin-left: 4px;
                                font-size: 16px;
                                font-weight: 600;
                                color: #2C50EE;
                                position: relative;
                                bottom: 8px;
                            }

                            .item-stat-label {
                                text-align: left;
                                font-size: 14px;
                                color: #9A9FAF;
                                line-height: 25px;
                            }
                        }
                    }

                    .item-label {
                        font-size: 20px;
                        font-weight: 600;
                        color: #535763;
                        margin-top: 18px;
                    }

                    &:nth-of-type( 1 ) {
                        width: 300px;

                        .item-stat {
                            justify-content: space-around;
                        }
                    }

                    &:nth-of-type( 2 ) {
                        width: 450px;
                    }

                    &:nth-of-type( 3 ) {
                        width: calc( 100% - 300px - 450px - 28px - 28px );
                        margin-right: 0;
                    }
                }
            }

            >.middle-panel {
                height: 600px;
                width: 100%;
                margin-bottom: 28px;

                >.panel-item {
                    display: inline-block;
                    vertical-align: top;
                    height: 100%;
                    margin-right: 28px;
                    background: #FFFFFF;
                    border-radius: 8px;
                    padding: 40px 40px 20px;
                    box-sizing: border-box;

                    >.panel-content {
                        padding-top: 14px;
                        height: calc( 100% - 28px );
                        box-sizing: border-box;

                        .chart-box {
                            width: 100%;
                            height: calc( 100% - 160px );
                        }
                    }

                    &:nth-of-type( 1 ) {
                        width: 778px;

                        .flow-box {
                            position: relative;
                            height: 160px;
                            width: 100%;
                            background-image: url("../../assets/images/cqg/img_flow_1.png");
                            background-size: 100% 100%;

                            >span:nth-of-type( 1 ) {
                                left: 341px;
                                top: 59px;
                            }

                            >span:nth-of-type( 2 ) {
                                left: 120px;
                                top: 122px;
                            }

                            >span:nth-of-type( 3 ) {
                                left: 431px;
                                top: 24px;
                            }
                        }
                    }

                    &:nth-of-type( 2 ) {
                        width: calc( 100% - 778px - 28px );
                        margin-right: 0;

                        .flow-box {
                            position: relative;
                            height: 160px;
                            width: 100%;
                            background-image: url("../../assets/images/cqg/img_flow_2.png");
                            background-size: 100% 100%;

                            >span:nth-of-type( 1 ) {
                                top: 48px;
                                left: 189px;
                            }

                            >span:nth-of-type( 2 ) {
                                top: 48px;
                                left: 540px;
                            }

                            >span:nth-of-type( 3 ) {
                                top: 114px;
                                left: 49px;
                            }

                            >span:nth-of-type( 4 ) {
                                top: 114px;
                                left: 401px;
                            }

                            >span:nth-of-type( 5 ) {
                                top: 13px;
                                left: 224px;
                            }

                            >span:nth-of-type( 6 ) {
                                top: 13px;
                                left: 576px;
                            }
                        }
                    }
                }
            }

            >.bottom-panel {
                height: 430px;
                width: 100%;
                background: #FFFFFF;
                border-radius: 8px;
                padding: 40px;
                box-sizing: border-box;

                >.panel-content {
                    padding-top: 14px;
                    height: calc( 100% - 28px );
                    box-sizing: border-box;

                    >.flow-box {
                        display: inline-block;
                        vertical-align: top;
                        width: 724px;
                        height: 100%;
                        margin-right: 28px;
                        background-image: url("../../assets/images/cqg/img_flow_3.png");
                        background-size: 100% 100%;
                        position: relative;

                        >span:nth-of-type( 1 ) {
                            top: 47px;
                            left: 189px;
                        }

                        >span:nth-of-type( 2 ) {
                            top: 47px;
                            left: 540px;
                        }

                        >span:nth-of-type( 3 ) {
                            top: 187px;
                            left: 189px;
                        }

                        >span:nth-of-type( 4 ) {
                            top: 109px;
                            left: 49px;
                        }

                        >span:nth-of-type( 5 ) {
                            top: 109px;
                            left: 401px;
                        }

                        >span:nth-of-type( 6 ) {
                            top: 248px;
                            left: 49px;
                        }

                        >span:nth-of-type( 7 ) {
                            top: 10px;
                            left: 224px;
                        }

                        >span:nth-of-type( 8 ) {
                            top: 10px;
                            left: 576px;
                        }

                        >span:nth-of-type( 9 ) {
                            top: 150px;
                            left: 224px;
                        }
                    }

                    >.chart-box {
                        display: inline-block;
                        vertical-align: top;
                        width: calc( 100% - 724px - 28px );
                        height: 100%;
                    }
                }
            }
        }
    }
</style>
